#ifndef __THREAD_POOL__
#define __THREAD_POOL__
#include <iostream>
#include <vector>
#include "Thread.hpp"
#include <queue>
#include <pthread.h>
const static int defaultThreadNum = 3;
using namespace ThreadModule;

// 日志
template <typename T>
class ThreadPool
{
private:
    int _threadnum;
    std::vector<Thread> _threads;
    std::queue<T> _task_queue;
    pthread_mutex_t _mutex;
    pthread_cond_t _cond;

    int _waitnum;
    bool _isrunning;

private:
    void LockQueue()
    {
        pthread_mutex_lock(&_mutex);
    }
    void UnLockQueue()
    {
        pthread_mutex_unlock(&_mutex);
    }
    void ThreadSleep()
    {
        pthread_cond_wait(&_cond, &_mutex);
    }
    void ThreadWakeup()
    {
        pthread_cond_signal(&_cond);
    }
    void ThreadWakeupAll()
    {
        pthread_cond_broadcast(&_cond);
    }
public:
    void HandlerTask(const std::string name)
    {
        while (true)
        {
            LockQueue();
            // 1.获取数据
            while (_task_queue.empty() && _isrunning)
            {
                _waitnum++;
                ThreadSleep();
                _waitnum--;
            }
            // 2.1如果线程池已经退出了 && 任务队列是空
            if (_task_queue.empty() && !_isrunning)
            {
                UnLockQueue();
                std::cout<< name <<"  quit...  "<<std::endl;
                break;
            }
            // 2.2线程池不退出 && 任务队列不是空

            // 2.一定有任务
            T t;
            t = _task_queue.front();
            _task_queue.pop();
            UnLockQueue();

            // 3.处理任务
            // t(); // 注意不能在加锁与解锁之间处理。
        }
    }
    void InitThreadPool()
    {
        // 构建出所有的线程
        for (int i = 0; i < _threadnum; i++)
        {
            std::string threadname = "thread-" + std::to_string(i + 1);
            // _threads.emplace_back(Print, threadname); // ??
            _threads.emplace_back(std::bind(&ThreadPool::HandlerTask, this, std::placeholders::_1),threadname);
        }
        _isrunning = true;

    }
    void Start()
    {
        for (auto &thread : _threads)
        {
            thread.Start();
        }
    }
    void Stop()
    {
        LockQueue();
        _isrunning = false;
        ThreadWakeupAll();
        UnLockQueue();
    }
    void Wait()
    {
        for (auto &thread : _threads)
        {
            thread.Join();
        }
    }
    bool Enqueue(const T &t)
    {
        bool ret = false;
        LockQueue();
        if (_isrunning)
        {
            _task_queue.push(t);
            if (_waitnum > 0)
            {
                // 此时就说明有线程在等待
                ThreadWakeup();
            }
            ret = true;
        }
        UnLockQueue();
        return ret;
    }

    ThreadPool(int threadnum = defaultThreadNum)
        : _threadnum(threadnum), _waitnum(0), _isrunning(false)
    {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_cond, nullptr);
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_cond);
    }
};

#endif